# -*- coding: utf-8 -*-
'''
Created on Nov 10, 2011

@author: onetera
'''
from PyQt4 import QtCore , QtGui
from lib.customUI import customUI
from lib.papyDB import papyDB , tableInfo
import os
papyDB = papyDB()


class taskList_UI(customUI):
    def __init__(self,parent=None):
        QtGui.QMainWindow.__init__(self , parent)
        customUI.__init__(self)
         
        
    def createUserInfo(self):
        self.userInfo_grp = self.grpBox("User Information", self.sizePolicy_fxfx )
        self.userInfo_grp.setMinimumWidth(400)        
        self.nLoginID_lb =self.label(self.userInfo_grp)
        self.nLoginID_lb.setText("LogIn ID : ") 
        self.loginID_lb = self.label(self.nLoginID_lb )
        self.loginID_lb.setText(os.environ['username'] )
        
        self.nMachine_lb =self.label(self.userInfo_grp)
        self.nMachine_lb.setText("Machine : ")        
        self.machine_lb = self.label(self.nLoginID_lb )
        self.machine_lb.setText(os.environ['computername'] )
        
        self.nAdminLev_lb =self.label(self.userInfo_grp)
        self.nAdminLev_lb.setText("Admin Level : ")
        self.adminLev_lb = self.label(self.nLoginID_lb )
        adminLevel = papyDB.getFetch('select adminLevel from USER_INFO where userLogInName="%s"' % os.environ['username'] )[0][0]
        self.adminLev_lb.setText( str(adminLevel) )
        layout = QtGui.QGridLayout()
        layout.addWidget(self.nLoginID_lb, 0 , 0)
        layout.addWidget(self.loginID_lb, 0 , 1)
        layout.addWidget(self.nMachine_lb, 0 , 2)
        layout.addWidget(self.machine_lb, 0 , 3)
        layout.addWidget(self.nAdminLev_lb, 0 , 4)
        layout.addWidget(self.adminLev_lb, 0 , 5)
        self.userInfo_grp.setLayout(layout)
        
    def createTaskList(self):
        self.taskList_grp,self.taskList_cb = self.grpCBox("Assigned Task")        
        query1 = "select assetName from ASSET where userID = %s" % papyDB.userID()
        taskList = [x[0] for x in papyDB.getFetch( query1 ) ]
        for x in taskList :    
            self.taskList_cb.addItem(x)        
        self.taskList_cb.currentIndexChanged.connect( self.changeInfo )
        
    def createDate(self):
        self.startDate_grp = self.grpBox("Start Date", self.sizePolicy_fxfx )
        self.endDate_grp = self.grpBox("End Date", self.sizePolicy_fxfx )    
        self.startDate_lb =self.label(self.startDate_grp)
        self.endDate_lb =self.label(self.endDate_grp)
        
    def changeInfo(self):        
        assetName = str( self.taskList_cb.currentText() )
        if assetName == '' :
            return        
        query1 = 'select assetStartDate from ASSET where userID = "%s" and assetName="%s"' % (papyDB.userID() ,assetName)   
        self.startDate_lb.setText( str( papyDB.getFetch(query1)[0][0] ) )
        query2 = 'select assetEndDate from ASSET where userID = "%s" and assetName="%s"' % (papyDB.userID() ,assetName)
        self.endDate_lb.setText( str( papyDB.getFetch(query2)[0][0] ) )
        query = 'select assetDesc from ASSET where userID = "%s" and assetName="%s"' % (papyDB.userID() ,assetName)
        txt = papyDB.getFetch(query)[0][0]        
        self.desc_txt.setText( txt )
        
    def createDateInfo(self):
        self.dateInfo_grp = self.grpBox("Date Info", self.sizePolicy_fxfx )
        self.nStartDate_lb =self.label(self.dateInfo_grp)
        self.nStartDate_lb.setText("Start Date : ") 
        self.startDate_lb =self.label(self.dateInfo_grp)
        self.startDate_lb.setText("Start Date") 
        
        spacer = QtGui.QSpacerItem(300, 20, QtGui.QSizePolicy.Expanding,
                                    QtGui.QSizePolicy.Minimum) 
        
        self.nEndDate_lb =self.label(self.dateInfo_grp)
        self.nEndDate_lb.setText("End Date : ")
        self.endDate_lb =self.label(self.dateInfo_grp)
        self.endDate_lb.setText("End Date")
        
        layout = QtGui.QGridLayout()
        layout.addWidget(self.nStartDate_lb, 0 , 0)        
        layout.addWidget(self.startDate_lb, 0 , 1)
        layout.addItem(spacer, 0 , 2)
        layout.addWidget(self.nEndDate_lb, 0 , 3)
        layout.addWidget(self.endDate_lb, 0 , 4)        
        self.dateInfo_grp.setLayout(layout)
        
    def createDesc(self):
        self.desc_txt = self.txtEditor()
        self.desc_txt.enabledChange(False)
#        self.desc_txt.enabledChange(False)
        self.desc_txt.setFont( self.txtEdtFont )
            
    def setUI(self):
        self.mainWidget = QtGui.QWidget( )
        self.createUserInfo()
        spacer = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding,
                                    QtGui.QSizePolicy.Minimum) 
        self.createTaskList()
#        self.createPrjList()
        self.createDateInfo()
        desc_lb = self.label(" [ Descripttion ] ")
        self.createDesc()
        
        mainGridLayout = QtGui.QGridLayout(self.mainWidget)
        mainGridLayout.addWidget(self.userInfo_grp, 0, 0, 1, 3)        
        mainGridLayout.addItem(spacer , 0, 5, 1, 1)
        
#        mainGridLayout.addWidget(self.prjList_grp, 1, 0, 1, 4)
        mainGridLayout.addWidget(self.taskList_grp, 0, 4, 1, 5)
        
        mainGridLayout.addWidget(self.dateInfo_grp, 1, 0, 1, 4)
        mainGridLayout.addWidget(desc_lb, 2, 0, 1, 1)
        
        mainGridLayout.addWidget(self.desc_txt, 3, 0, 1, 6)
        
        self.setCentralWidget(self.mainWidget)
        self.setWindowTitle("Asset Task List")
        self.setGeometry(QtCore.QRect(50,600,400, 300))
        self.show()
        self.changeInfo()


        
if __name__ == '__main__':
    import sys    
    app = QtGui.QApplication(sys.argv)
    ui = taskList_UI()
    ui.setUI()
    app.exec_()         
        
        
        
        
        
        